// axios的配置
import axios from "axios";
//引入ElementUI中的全局提示方法
import { MessageBox, Message } from "element-ui"

//引入进度条
import nprogress from "nprogress"
let http = axios.create({
    withCredentials: true, //是否携带凭证
    baseURL: "/api", //基础地址
    timeout: 5000, //超时设置
})
// console.log(MessageBox)
// 请求拦截
http.interceptors.request.use(config => {
    //把进度条打开
    nprogress.start()
    let token = localStorage.getItem('myx-token');
    config.headers.authorization = token;
    return config
})

// 响应拦截器
http.interceptors.response.use(config => {
    //停止进度条
    nprogress.done()
    let {
        data: {
            state,
            msg,
            code
        }
    } = config;
    let codes = ['10022', '1004', '10026']
    if (codes.includes(code)) {
        //清除token 
        localStorage.removeItem('myx-token')
        //刷新页面
        location.reload()
    }

    //排除不弹窗的接口
    let excludeUrl = ["/order/query_order_status"]

    //判断不需要弹窗的数组中是否包含当前请求的路径
    let isExclude = excludeUrl.includes(config.config.url)
    // console.log(config)
    //拦截器判断后端响应的state
    if (!state && !isExclude) {
        MessageBox.alert(msg, '错误提示', {
            type: "error"
        });
    }
    return config
})
export default http;